﻿using SvGame.Objects;

namespace SvGame.Sv;

public static class GameTcp
{
    public static async ValueTask OnConnected(IAppSession session)
    {
        //注册用于处理连接、关闭的Session处理器
        //Console.WriteLine($"session connected: {session.RemoteEndPoint}");
        ((MySession)session).IsConnected = true;

        //发送消息给客户端
        var msg = new G2CConnect();
        var pack = MyPackage.From(msg);
        //loger.Debug($"pack.ProtocolId {pack.ProtocolId}");
        await session.SendAsync(pack.Encode());
        //var msg = $@"Welcome to TelnetServer: {session.RemoteEndPoint}";
        //await session.SendAsync(Encoding.UTF8.GetBytes(msg + "\r\n"));
    }
    public static async ValueTask OnClosed(IAppSession session, CloseEventArgs reason)
    {
        var mySession = (MySession)session;
        mySession.IsConnected = false;
        await Task.Run(() => {
            var player = mySession.GetObj<Player>();
            if (player != null)
            {
                player.Online = false;
                player.TicksDisconnect = DateTime.Now.Ticks;
            }
        });
        //Console.WriteLine($"session {session.RemoteEndPoint} closed: {reason}");
    }
}
